package com.tongcao.cn.inteceptor;

import com.tongcao.cn.annotation.OperAction;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:tongcao
 * @Date:2019/1/10 14:37
 * Description:
 */
public class OperInteceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 如果不是映射到方法,则直接通过
         */
        if (handler instanceof HandlerMethod) {
            HandlerMethod action = (HandlerMethod) handler;
            OperAction operAction = action.getMethodAnnotation(OperAction.class);
            if (operAction != null) {
                String value = operAction.value();
                //对访问操作进行处理
                System.out.println("注解拦截器生效中value:" + value);
                return true;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
